#include <iostream>
#include <deque>

#include "Image.H"
#include "Util.H"
#include "IO.H"
#include "rgm.h"

using namespace std;
using namespace pip;

int main(int argc, char *argv[])
{
  if (argc < 4) {        //get arguments
	  error("", "Usage: ccl -i <input image> -connectivity <{4,6,8,18,26}> \n");
  }
  vector<string> args(argv, argv + argc);


  Image<float> in_float;      //get vars
  Image<short> in_short;
  Image<unsigned char> in_char;
  Image<Color<unsigned char> > in_color;
  Image<float> out;

  string inputname;
  int n(0);
  vector<int> objects;

  getScalarArg(args, "-i", inputname);     // init vars with argument if possible
  getScalarArg(args, "-connectivity", n);

  if (importFile(in_float, inputname))   // call ccl with correct image_type
  {
      connectedComponentLabeling(in_float , out, n, objects);
  } else if (importFile(in_short, inputname)) 
  {
      connectedComponentLabeling(in_short , out, n, objects);
  } else if (importFile(in_char, inputname)) 
  {
	  connectedComponentLabeling(in_char , out, n, objects);
  } else if (importFile(in_color, inputname)) 
  {
	  connectedComponentLabeling(in_color , out, n, objects);
  } else 
  {   
	  cout << "The inputimage is not an Image of pixeltype: [unsigned char(also colored), short or float] !\n";
      //return 4;
  }

  // print table... run through ordered vector from the end ...
  cout << "In total there are " << objects.size() << " objects in the image.\nHere are the ten largest objects:\n";
  for (unsigned int i= objects.size()-1; ( (i >= objects.size()-10) && (i >= 0) ) ;--i) 
  {
	  cout << "Object #" << objects.size()-i << " counts\t " << objects[i] << " pixel...\n";
  }

  //save output of ccl
  if (!exportFile(out, "output_ccl.pip"))
	  cout << "unable to write output file onto disk...\n";
system("pause");

}
